<template>
	<view>
		<u-cell-group class="flex-padd">
			<!-- input框不带图标 -->
			<u-cell-item title="条形码:" :arrow="false" class="list-title">
				<view class="list-text" style="display: flex;">
					<input class="list-text-input" v-model="inputInfo.barCode" />
				</view>
			</u-cell-item>

			<u-cell-item title="设备编码:" :arrow="false" class="list-title">
				<input class="list-text" v-model="inputInfo.deviceCode" />
			</u-cell-item>
			<u-cell-item title="设备名称:" :arrow="false" class="list-title">
				<input class="list-text" v-model="inputInfo.deviceName" />
			</u-cell-item>
			<u-cell-item title="规格:" :arrow="false" class="list-title">
				<input class="list-text" v-model="inputInfo.deviceFormat" />
			</u-cell-item>
			<u-cell-item title="型号:" :arrow="false" class="list-title">
				<input class="list-text" v-model="inputInfo.deviceModel" />
			</u-cell-item>
			<u-cell-item title="盘点前管理部门:" :arrow="false" class="list-title">
				<input class="list-text" v-model="inputInfo.checkManageDeptBeforeName" />
			</u-cell-item>
			<u-cell-item title="盘点后管理部门:" :arrow="false" class="list-title">
				<input class="list-text" v-model="inputInfo.checkManageDeptAfterName" />
			</u-cell-item>
			<u-cell-item title="盘点前使用部门:" :arrow="false" class="list-title">
				<input class="list-text" v-model="inputInfo.checkUseDeptBeforeName" />
			</u-cell-item>
			<u-cell-item title="盘点后使用部门:" :arrow="false" class="list-title">
				<input class="list-text" v-model="isNUll(inputInfo.checkUseDeptAfterName) ? inputInfo.checkUseDeptAfterName = inputInfo.checkUseDeptBeforeName : inputInfo.checkUseDeptAfterName" />
			</u-cell-item>
			<u-cell-item title="盘点前位置:" :arrow="false" class="list-title">
				<input class="list-text" v-model="inputInfo.checkPositionBeforeName" />
			</u-cell-item>
			<u-cell-item title="盘点后位置:" :arrow="false" class="list-title">
				<input class="list-text" v-model="isNUll(inputInfo.checkPositionAfterName) ? inputInfo.checkPositionAfterName = inputInfo.checkPositionBeforeName : inputInfo.checkPositionAfterName" />
			</u-cell-item>
			<u-cell-item title="盘点前使用权:" :arrow="false" class="list-title">
				<input class="list-text" v-model="inputInfo.checkUseRightBeforeName" />
			</u-cell-item>
			<u-cell-item title="盘点后使用权:" :arrow="false" class="list-title">
				<input class="list-text" v-model="isNUll(inputInfo.checkUseRightAfterName) ? inputInfo.checkUseRightAfterName = inputInfo.checkUseRightBeforeName : inputInfo.checkUseRightAfterName" />
			</u-cell-item>
			<u-cell-item title="盘点前责任人:" :arrow="false" class="list-title">
				<input class="list-text" v-model="inputInfo.checkLiablePersonBeforeName" />
			</u-cell-item>
			<u-cell-item title="盘点后责任人:" :arrow="false" class="list-title">
				<input class="list-text" v-model="isNUll(inputInfo.checkLiablePersonAfterName) ? inputInfo.checkLiablePersonAfterName = inputInfo.checkLiablePersonBeforeName : inputInfo.checkUseDeptAfterName" />
			</u-cell-item>
			<u-cell-item title="盘点前设备状态:" :arrow="false" class="list-title">
				<input class="list-text" v-model="inputInfo.checkDeviceStateBeforeName" />
			</u-cell-item>
			<u-cell-item title="盘点后设备状态:" :arrow="false" class="list-title">
				<input class="list-text" v-model="isNUll(inputInfo.checkDeviceStateAfterName) ? inputInfo.checkDeviceStateAfterName = inputInfo.checkDeviceStateBeforeName : inputInfo.checkDeviceStateAfterName" />
			</u-cell-item>
		</u-cell-group>


		<!-- 按钮 -->
		<view class="flexinven-button">
			<u-row gutter="16" class="flexinven-con">
				<u-col span="4">
					<view class="flexinven-flex" @click="eamDevicaCheckDetails">列表</view>
				</u-col>
				<u-col span="4">
					<view class="flexinven-flex" @click="doSubmit">确定</view>
				</u-col>
				<u-col span="4">
					<view class="flexinven-flex" @click="eamDevicaCheckList">返回</view>
				</u-col>
			</u-row>
		</view>
	</view>
</template>

<script>
	let _this;
	var main, receiver, filter;
	var _codeQueryTag = false;
	var Utils = uni.requireNativePlugin("UtilsModulePlugin")
	export default {
		data() {
			return {
				instFlag: true,
				checkBill: '',
				checkId: '',
				code: '',
				treeData: [],
				inputInfo: {
					id: '',
					checkId: '', // 盘点单id
					deviceId: '', // 设备卡片id
					deviceCode: '', // 设备编号
					deviceName: '', // 设备名称
					deviceType: '', // 设备类别
					barCode: '', // 条形码
					deviceFormat: '', // 规格
					deviceModel: '', // 型号
					handleFlag: '', // 是否已处理
					checkFlag: false, // 是否盘到
					checkResult: '', // 盘点结果
					checkManageDeptBefore: '', // 盘点前管理部门
					checkManageDeptBeforeCode: '', // 盘点前管理部门编码
					checkManageDeptBeforeName: '', // 盘点前管理部门名称
					checkManageDeptAfter: '', // 盘点后管理部门
					checkManageDeptAfterCode: '', // 盘点后管理部门编码
					checkManageDeptAfterName: '', // 盘点后管理部门名称
					checkUseDeptBefore: '', // 盘点前使用部门
					checkUseDeptBeforeCode: '', // 盘点前使用部门编码
					checkUseDeptBeforeName: '', // 盘点前使用部门名称
					checkUseDeptAfter: '', // 盘点后使用部门
					checkUseDeptAfterCode: '', // 盘点后使用部门编码
					checkUseDeptAfterName: '', // 盘点后使用部门名称
					checkUseRightBefore: '', // 盘点前使用权
					checkUseRightBeforeCode: '', // 盘点前使用权编码
					checkUseRightBeforeName: '', // 盘点前使用权名称
					checkUseRightAfter: '', // 盘点后使用权
					checkUseRightAfterCode: '', // 盘点后使用权编码
					checkUseRightAfterName: '', // 盘点后使用权名称
					checkLiablePersonBefore: '', // 盘点前责任人
					checkLiablePersonBeforeCode: '', // 盘点前责任人编码
					checkLiablePersonBeforeName: '', // 盘点前责任人名称
					checkLiablePersonAfter: '', // 盘点后责任人
					checkLiablePersonAfterCode: '', // 盘点后责任人编码
					checkLiablePersonAfterName: '', // 盘点后责任人名称
					checkPositionBefore: '', // 盘点前位置
					checkPositionBeforeCode: '', // 盘点前位置编码
					checkPositionBeforeName: '', // 盘点前位置名称
					checkPositionAfter: '', // 盘点后位置
					checkPositionAfterCode: '', // 盘点后位置编码
					checkPositionAfterName: '', // 盘点后位置
					checkDeviceStateBefore: '', // 盘点前设备状态
					checkDeviceStateBeforeCode: '', // 盘点前设备状态编码
					checkDeviceStateBeforeName: '', // 盘点前设备状态名称
					checkDeviceStateAfter: '', // 盘点后设备状态
					checkDeviceStateAfterCode: '', // 盘点后设备状态编码
					checkDeviceStateAfterName: '', // 盘点后设备状态名称
					dealResult: '', // 处理结果
					checkBy: '', // 盘点人
					checkByName: '',
					checkDate: '', // 盘点日期
					capitalOrganize: '', // 资产组织
					createBy: {
						id: ''
					},
					createDate: '',
					updateBy: {
						id: ''
					},
					updateDate: '',
					remarks: ''
				}
			}
		},
		onLoad(option) {
			console.log("onLoad")
			this.initScan(option)
			this.ftreeData()
		},
		onShow() {
			console.log("onShow")
			this.startScan();
		},
		onHide() {
			console.log("onHide")
			this.stopScan();
		},
		onUnload() {
			console.log("onUnload")
			this.stopScan();
		},
		methods: {
			// 初始化监听
			initScan(option) {
				_this = this;
				this.checkBill = option.checkBill
				this.checkId = option.checkId
				// #ifdef APP-PLUS
				main = plus.android.runtimeMainActivity(); //获取activity  
				var context = plus.android.importClass('android.content.Context'); //上下文  
				receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
					onReceive: doReceive
				});
				var IntentFilter = plus.android.importClass('android.content.IntentFilter');
				var Intent = plus.android.importClass('android.content.Intent');
				filter = new IntentFilter();
				//针对优博讯安卓PDA-i6300A添加监听，其它优博讯的型号应该一样或类似
				filter.addAction("android.intent.ACTION_DECODE_DATA"); //监听扫描

				main.registerReceiver(receiver, filter); //注册监听  

				function doReceive(context, intent) {
					//通过intent实例引入intent类，方便以后的‘.’操作  
					plus.android.importClass(intent);
					console.log('queryCode')
					// 条码
					_this.queryCode(context, intent);
					//main.unregisterReceiver(receiver);//取消监听  
				}
				// #endif
			},
			// 注册监听  
			startScan() {
				// #ifdef APP-PLUS
				main.registerReceiver(receiver, filter);
				// #endif
			},
			// 取消监听
			stopScan() {
				// #ifdef APP-PLUS
				main.unregisterReceiver(receiver);
				// #endif
			},
			queryCode: function(context, intent) {
				//防重复  
				if (_codeQueryTag) return false;
				_codeQueryTag = true;
				setTimeout(function() {
					//条码内容
					var barcodeBytes = intent.getByteArrayExtra("barcode");
					//条码长度
					var barcodeLength = intent.getIntExtra("length", 0);
					//条码类型
					var barcodeTypeBytes = intent.getByteExtra("barcodeType", (0 | 0));
					console.log('byteToString')
					_this.byteToString(new String(barcodeBytes), barcodeLength)
					_codeQueryTag = false;
				}, 150);
			},
			eamDevicaCheckDetails() {
				uni.redirectTo({
					url: '/pages/eam/eamDeviceCheck/eamDevicaCheckDetails?checkBill=' + this.checkBill + '&checkId=' + this.checkId
				});
			},
			eamDevicaCheckList() {
				uni.switchTab({
					url: '/pages/eam/eamDeviceCheck/eamDeviceCheckList'
				});
			},
			isNUll(v) {
				if (v === null || v === undefined || v === '') {
					return true
				}
				return false
			},
			byteToString(v, l) {
				Utils.byteToString({
					"byte": v,
					"len": l
				}, (res) => {
					_this.code = res.res
					console.log(_this.code)
					_this.infoValue()
				})
			},
			clearInputInfo() {
				return {
					id: '',
					checkId: '', // 盘点单id
					deviceId: '', // 设备卡片id
					deviceCode: '', // 设备编号
					deviceName: '', // 设备名称
					deviceType: '', // 设备类别
					barCode: '', // 条形码
					deviceFormat: '', // 规格
					deviceModel: '', // 型号
					handleFlag: '', // 是否已处理
					checkFlag: false, // 是否盘到
					checkResult: '', // 盘点结果
					checkManageDeptBefore: '', // 盘点前管理部门
					checkManageDeptBeforeCode: '', // 盘点前管理部门编码
					checkManageDeptBeforeName: '', // 盘点前管理部门名称
					checkManageDeptAfter: '', // 盘点后管理部门
					checkManageDeptAfterCode: '', // 盘点后管理部门编码
					checkManageDeptAfterName: '', // 盘点后管理部门名称
					checkUseDeptBefore: '', // 盘点前使用部门
					checkUseDeptBeforeCode: '', // 盘点前使用部门编码
					checkUseDeptBeforeName: '', // 盘点前使用部门名称
					checkUseDeptAfter: '', // 盘点后使用部门
					checkUseDeptAfterCode: '', // 盘点后使用部门编码
					checkUseDeptAfterName: '', // 盘点后使用部门名称
					checkUseRightBefore: '', // 盘点前使用权
					checkUseRightBeforeCode: '', // 盘点前使用权编码
					checkUseRightBeforeName: '', // 盘点前使用权名称
					checkUseRightAfter: '', // 盘点后使用权
					checkUseRightAfterCode: '', // 盘点后使用权编码
					checkUseRightAfterName: '', // 盘点后使用权名称
					checkLiablePersonBefore: '', // 盘点前责任人
					checkLiablePersonBeforeCode: '', // 盘点前责任人编码
					checkLiablePersonBeforeName: '', // 盘点前责任人名称
					checkLiablePersonAfter: '', // 盘点后责任人
					checkLiablePersonAfterCode: '', // 盘点后责任人编码
					checkLiablePersonAfterName: '', // 盘点后责任人名称
					checkPositionBefore: '', // 盘点前位置
					checkPositionBeforeCode: '', // 盘点前位置编码
					checkPositionBeforeName: '', // 盘点前位置名称
					checkPositionAfter: '', // 盘点后位置
					checkPositionAfterCode: '', // 盘点后位置编码
					checkPositionAfterName: '', // 盘点后位置
					checkDeviceStateBefore: '', // 盘点前设备状态
					checkDeviceStateBeforeCode: '', // 盘点前设备状态编码
					checkDeviceStateBeforeName: '', // 盘点前设备状态名称
					checkDeviceStateAfter: '', // 盘点后设备状态
					checkDeviceStateAfterCode: '', // 盘点后设备状态编码
					checkDeviceStateAfterName: '', // 盘点后设备状态名称
					dealResult: '', // 处理结果
					checkBy: '', // 盘点人
					checkByName: '',
					checkDate: '', // 盘点日期
					capitalOrganize: '', // 资产组织
					createBy: {
						id: ''
					},
					createDate: '',
					updateBy: {
						id: ''
					},
					updateDate: '',
					remarks: ''
				}
			},
			infoValue() {
				if (this.code.indexOf("\r\n") !== -1) {
					console.log(this.code.split("\r\n")[0])
					this.code = this.code.split("\r\n")[0].split(":")[1]
				}
				console.log(this.checkBill)
				console.log(this.code)
				plus.sqlite.selectSql({
					name: 'first',
					sql: "SELECT * FROM EamDeviceCheck a LEFT JOIN EamDeviceCheckDetails b on a.id = b.checkId WHERE a.checkBill = '" +
						this.checkBill + "' and b.deviceCode = '" + this.code + "'",
					success: function(data) {
						console.log(data)
						if (data.length <= 0) {
							_this.instFlag = true
							_this.inputInfo = _this.clearInputInfo()
							_this.inputInfo.deviceCode = _this.code
							console.log(_this.inputInfo)
							uni.showModal({
								content: '当前盘点单没有查询到条码对应的卡片数据',
								showCancel: false
							});
						} else {
							_this.instFlag = false
							_this.inputInfo = data[0]
						}
					},
					fail: function(e) {
						console.log('findList failed: ' + JSON.stringify(e));
					}
				});
			},
			ftreeData() {
				var data = this.treeData.filter(item => item.parent === "0")
				data.forEach((item, index) => {
					console.log(index)
					item.children = _this.treeData.filter(fitem => item.id === fitem.parent)
				})
				console.log(this.treeData)
				// plus.sqlite.selectSql({
				// 	name: 'first',
				// 	sql: "SELECT * FROM PdDutyCore",
				// 	success: function(data) {
				// 		var dd = _this.$utils.treeDataTranslate(data)

				// 		console.log(dd)
				// 		for(var i in dd){
				// 			console.log(data[i])
				// 		}
				// 	},
				// 	fail: function(e) {
				// 		console.log('findList failed: ' + JSON.stringify(e));
				// 	}
				// });
			},
			doSubmit() {
				console.log("确定", this.inputInfo)
				if (this.instFlag) {
					let sql =
						"insert into EamDeviceCheckDetails (checkId,deviceCode,deviceName,barCode,deviceFormat,deviceModel,checkResult,checkManageDeptBeforeName,checkManageDeptAfterName,checkUseDeptBeforeName,checkUseDeptAfterName,checkUseRightBeforeName,checkUseRightAfterName,checkLiablePersonBeforeName,checkLiablePersonAfterName,checkPositionBeforeName,checkPositionAfterName,checkDeviceStateBeforeName,checkDeviceStateAfterName) VALUES (" +
						"'" + this.checkId + "'," +
						"'" + this.inputInfo.deviceCode + "'," +
						"'" + this.inputInfo.deviceName + "'," +
						"'" + this.inputInfo.barCode + "'," +
						"'" + this.inputInfo.deviceFormat + "'," +
						"'" + this.inputInfo.deviceModel + "'," +
						"'2'," +
						"'" + this.inputInfo.checkManageDeptBeforeName + "'," +
						"'" + this.inputInfo.checkManageDeptAfterName + "'," +
						"'" + this.inputInfo.checkUseDeptBeforeName + "'," +
						"'" + this.inputInfo.checkUseDeptAfterName + "'," +
						"'" + this.inputInfo.checkUseRightBeforeName + "'," +
						"'" + this.inputInfo.checkUseRightAfterName + "'," +
						"'" + this.inputInfo.checkLiablePersonBeforeName + "'," +
						"'" + this.inputInfo.checkLiablePersonAfterName + "'," +
						"'" + this.inputInfo.checkPositionBeforeName + "'," +
						"'" + this.inputInfo.checkPositionAfterName + "'," +
						"'" + this.inputInfo.checkDeviceStateBeforeName + "'," +
						"'" + this.inputInfo.checkDeviceStateAfterName + "')"
					console.log(sql)
					plus.sqlite.executeSql({
						name: 'first',
						sql: sql,
						success: function(e) {
							console.log('盘点卡片 insert success!');
						},
						fail: function(e) {
							console.log('盘点卡片 insert failed: ' + JSON.stringify(e));
						}
					});
				} else {
					let state = '1' // 相符
					if ((this.inputInfo.checkManageDeptBeforeName != this.inputInfo.checkManageDeptAfterName) ||
						(this.inputInfo.checkUseDeptBeforeName != this.inputInfo.checkUseDeptAfterName) ||
						(this.inputInfo.checkUseRightBeforeName != this.inputInfo.checkUseRightAfterName) ||
						(this.inputInfo.checkLiablePersonBeforeName != this.inputInfo.checkLiablePersonAfterName) ||
						(this.inputInfo.checkPositionBeforeName != this.inputInfo.checkPositionAfterName) ||
						(this.inputInfo.checkDeviceStateBeforeName != this.inputInfo.checkDeviceStateAfterName)
					) {
						state = '3' // 差异
					}
					let sql = "UPDATE EamDeviceCheckDetails SET " +
						"deviceCode = '" + this.inputInfo.deviceCode + "'," +
						"deviceName = '" + this.inputInfo.deviceName + "'," +
						"barCode = '" + this.inputInfo.barCode + "'," +
						"deviceFormat = '" + this.inputInfo.deviceFormat + "'," +
						"deviceModel = '" + this.inputInfo.deviceModel + "'," +
						"checkResult = '" + state + "'," +
						"checkManageDeptBeforeName = '" + this.inputInfo.checkManageDeptBeforeName + "'," +
						"checkManageDeptAfterName = '" + this.inputInfo.checkManageDeptAfterName + "'," +
						"checkUseDeptBeforeName = '" + this.inputInfo.checkUseDeptBeforeName + "'," +
						"checkUseDeptAfterName = '" + this.inputInfo.checkUseDeptAfterName + "'," +
						"checkUseRightBeforeName = '" + this.inputInfo.checkUseRightBeforeName + "'," +
						"checkUseRightAfterName = '" + this.inputInfo.checkUseRightAfterName + "'," +
						"checkLiablePersonBeforeName = '" + this.inputInfo.checkLiablePersonBeforeName + "'," +
						"checkLiablePersonAfterName = '" + this.inputInfo.checkLiablePersonAfterName + "'," +
						"checkPositionBeforeName = '" + this.inputInfo.checkPositionBeforeName + "'," +
						"checkPositionAfterName = '" + this.inputInfo.checkPositionAfterName + "'," +
						"checkDeviceStateBeforeName = '" + this.inputInfo.checkDeviceStateBeforeName + "'," +
						"checkDeviceStateAfterName ='" + this.inputInfo.checkDeviceStateAfterName + "'" +
						" WHERE checkId = '" + this.inputInfo.checkId + "' and deviceCode = '" + this.inputInfo.deviceCode + "'"
					console.log(sql)
					plus.sqlite.executeSql({
						name: 'first',
						sql: sql,
						success: function(e) {
							console.log('盘点卡片 update success!');
						},
						fail: function(e) {
							console.log('盘点卡片 update failed: ' + JSON.stringify(e));
						}
					});
				}
				// 清空数据
				_this.inputInfo = _this.clearInputInfo()
			}

		}
	}
</script>

<style lang="less" scoped>
	.list-title {
		color: #799cb3;
	}
</style>
